<?php

namespace App\Http\Controllers\Api;

use App\Http\Controllers\Controller;
use App\Models\Vip;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;
class UserController extends Controller
{
    public function vip_Upgrade(Request $request)
    {

        $user = auth()->user();
        $vip_id=$request->vip_id;
        $vip=Vip::where('id',$vip_id)->first();
        //判断余额 是否充足
        if($user->balance<$vip->price){
            return response()->json(['status'=>'0','msg'=>'余额不足']);
        }
        //扣除余额
        $user->balance=$user->balance-$vip->price;
        $user->save();
        //升级vip
        $user->vip_id=$vip_id;
        $user->save();
        return response()->json(['status'=>'1','msg'=>'升级成功']);
    }
    public function dwz_exchange(Request $request)
    {
       $user = auth()->user();
       $vip=Vip::where('id',$user->vip_id)->first();
       //判断余额 是否充足
       $quantity=$request->quantity;
       if($quantity<1){
           return response()->json(['status'=>'0','msg'=>'请输入数量']);
       }
       //验证数量是不是正整数
       if(!preg_match('/^[1-9]\d*$/',$quantity)){
           return response()->json(['status'=>'0','msg'=>'请输入正整数']);
       }
       if($user->balance<$vip->dwz*$quantity){
           return response()->json(['status'=>'0','msg'=>'余额不足']);
       }
       $user->balance=$user->balance-$vip->dwz*$quantity;
       $user->dwz=$user->dwz+$quantity;
       $user->save();
       return response()->json(['status'=>'1','msg'=>'兑换成功']);
    }
    public function forward_exchange(Request $request)
    {
       $user = auth()->user();
       $vip=Vip::where('id',$user->vip_id)->first();
       
       $quantity=$request->quantity;
       if($quantity<1){
           return response()->json(['status'=>'0','msg'=>'请输入数量']);
       }
       //验证数量是不是正整数
       if(!preg_match('/^[1-9]\d*$/',$quantity)){
           return response()->json(['status'=>'0','msg'=>'请输入正整数']);
       }
       //判断余额 是否充足
       if($user->balance<$vip->forward*$quantity){
           return response()->json(['status'=>'0','msg'=>'余额不足']);
       }
       $user->balance=$user->balance-$vip->forward*$quantity;
       $user->forward=$user->forward+$quantity;
       $user->save();
       return response()->json(['status'=>'1','msg'=>'兑换成功']);
    }
    //  修改密码
    public function updatePassword(Request $request)
    {
        $user = auth()->user();
        $current_password = $request->current_password;
        $confirm_password = $request->confirm_password;
        //验证两次密码是否一样
       
        $password = $request->new_password;
        if ($confirm_password != $password) {
            return response()->json(['status'=>'0','msg'=>'两次密码不一致']);
        }
        //新密码必须是6位以上
        if (strlen($password) < 6) {
            return response()->json(['status'=>'0','msg'=>'新密码必须是6位以上']);
        }
        if (!Hash::check($current_password, $user->pass)) {
            return response()->json(['status'=>'0','msg'=>'当前密码错误']);
        } else {
            $user->pass = Hash::make($password);
            $user->updated_at = now();
            $user->save();
            return response()->json(['status'=>'1','msg'=>'修改成功']);
        }
    }
}
